<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Order extends CI_Controller{
	function __construct(){
	
		parent::__construct();
		$this->load->model('Order_model');
		$this->load->model('Produk_model');
		$this->load->model('Logistik_model');
		$this->load->library('form_validation');
		$this->load->library('email');
		if(!$this->session->userdata('user_id') && !$this->session->userdata('user_display_name') ):
		redirect('backend');
		endif;
	}

	function index(){
		$offset = $this->uri->segment(4);

		$this->load->library('pagination');
		$config['base_url'] = base_url().'/backend/order/index';
		$config['total_rows'] = count($this->Order_model->getOrders('all'));
		$config['per_page'] = $this->config->item('per_page');
		$config['uri_segment'] = '4';
		
		$data['title'] = 'All Order';
		$data['template'] = 'order/index';
		$data['res'] = $this->Order_model->getOrders('all',$config['per_page'],$offset);
		$data['urut'] = $this->uri->segment(4);
		$data['total_orders'] = count($this->Order_model->getOrders('all'));
		$data['total_new'] = count($this->Order_model->getOrders('neworder',FALSE,FALSE));
		$data['total_confirmed'] = count($this->Order_model->getOrders('confirmed',FALSE,FALSE));
		$data['total_pending'] = count($this->Order_model->getOrders('pending',FALSE,FALSE));
		$data['total_shipped'] = count($this->Order_model->getOrders('shipped',FALSE,FALSE));
		$data['total_closed'] = count($this->Order_model->getOrders('closed',FALSE,FALSE));
		$data['total_trashed'] = count($this->Order_model->getOrders('trashed',FALSE,FALSE));
		
		$this->pagination->initialize($config);
		$this->load->view('backend/index',$data);
		
	}
	
	function search_result(){
		
		if (($this->input->post('nama') == '') && ($this->input->post('order_code') == '') )
		{
			redirect('backend/order');
		}
			
		elseif ($this->input->post('order_code') != '')
		{
				$offset = $this->uri->segment(4);
				$this->load->library('pagination');
				$config['base_url'] = base_url().'/backend/order/search_result';
				$config['total_rows'] = count($this->Order_model->getCariOrder('code'));
				$config['per_page'] = $this->config->item('per_page');
				$config['uri_segment'] = '4';
				$data['urut'] = $this->uri->segment(4);
				$data['title'] = 'Hasil Cari';
				$data['res'] = $this->Order_model->getCariOrder('code',$config['per_page'],$offset,$this->input->post('order_code'),FALSE);
				$data['template'] = 'order/search_result';
				$this->pagination->initialize($config);
				$this->load->view('backend/index',$data);
		}
		elseif ($this->input->post('nama') != '')
		{
				$offset = $this->uri->segment(4);
				$this->load->library('pagination');
				$config['base_url'] = base_url().'/backend/order/search_result';
				$config['total_rows'] = count($this->Order_model->getCariOrder('nama'));
				$config['per_page'] = $this->config->item('per_page');
				$config['uri_segment'] = '4';
				$data['urut'] = $this->uri->segment(4);
				$data['title'] = 'Hasil Cari';
				$data['res'] = $this->Order_model->getCariOrder('nama',$config['per_page'],$offset,FALSE,$this->input->post('nama'));
				$data['template'] = 'order/search_result';
				$this->pagination->initialize($config);
				$this->load->view('backend/index',$data);
		}
		elseif (($this->input->post('nama') != '') && ($this->input->post('order_code') != ''))
		{
				$offset = $this->uri->segment(4);
				$this->load->library('pagination');
				$config['base_url'] = base_url().'/backend/order/search_result';
				$config['total_rows'] = count($this->Order_model->getCariOrder(FALSE,FALSE,FALSE,$this->input->post('order_code'),$this->input->post('nama')));
				$config['per_page'] = $this->config->item('per_page');
				$config['uri_segment'] = '4';
				$data['urut'] = $this->uri->segment(4);
				$data['title'] = 'Hasil Cari';
				$data['res'] = $this->Order_model->getCariOrder(FALSE,$config['per_page'],$offset,$this->input->post('order_code'),$this->input->post('nama'));
				$data['template'] = 'order/search_result';
				$this->pagination->initialize($config);
				$this->load->view('backend/index',$data);
		}
	}
	
	function status()
	{
		$offset = $this->uri->segment(5);
		$status = $this->uri->segment(4);

		$this->load->library('pagination');
		$config['base_url'] = base_url().'/backend/order/status/'.$status;
		$config['total_rows'] = count($this->Order_model->getOrders($status,FALSE,FALSE));
		$config['per_page'] = $this->config->item('per_page');
		$config['uri_segment'] = '5';
		$this->pagination->initialize($config);
		
		if ($status=='neworder') {
		$data['title'] = 'New Order';
		}
		else if ($status=='confirmed') {
		$data['title'] = 'Confrimed Order';
		}
		else if ($status=='pending') {
		$data['title'] = 'Pending Order';
		}
		else if ($status=='shipped') {
		$data['title'] = 'Shipped Order';
		}
		else if ($status=='closed') {
		$data['title'] = 'Closed Order';
		}
		else if ($status=='trashed') {
		$data['title'] = 'Trashed Order';
		}
		
		$data['template'] = 'order/index';
		$data['urut'] = $this->uri->segment(4);
		$data['res'] = $this->Order_model->getOrders($status,$config['per_page'],$offset);
		$data['total_orders'] = count($this->Order_model->getOrders('all'));
		$data['total_new'] = count($this->Order_model->getOrders('neworder',FALSE,FALSE));
		$data['total_confirmed'] = count($this->Order_model->getOrders('confirmed',FALSE,FALSE));
		$data['total_pending'] = count($this->Order_model->getOrders('pending',FALSE,FALSE));
		$data['total_shipped'] = count($this->Order_model->getOrders('shipped',FALSE,FALSE));
		$data['total_closed'] = count($this->Order_model->getOrders('closed',FALSE,FALSE));
		$data['total_trashed'] = count($this->Order_model->getOrders('trashed',FALSE,FALSE));
		
		$data['current'] = $status;
		$this->load->view('backend/index',$data);

	}

	function detail()
	{
		$this->form_validation->set_rules('status', 'Status','required');
		$this->form_validation->set_rules('bank_id', 'Bank','required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$id = $this->uri->segment(4);
			$msg = $this->uri->segment(5);
			$data['title'] = 'Order';
			$data['template'] = 'order/detail';
			if ($msg != 'success'):
				$data['pesan']='';
			else:
				$data['pesan']='<div id="kotak" class="success" >Data berhasil di update</div>';
			endif;
			$data['row'] = $this->Order_model->getOrderBy('by_id',$id,FALSE);
			$data['res_det'] = $this->Order_model->getOrderDetailById($id);
			$data['row_log'] = $this->Logistik_model->getLogistik(FALSE,$data['row']->logistik_id,FALSE,FALSE);
			$this->load->view('backend/index',$data);
		}
		else
		{
			$id = $this->uri->segment(4);
			$data = array
				(
				'order_status'=>$this->input->post('status'),
				'order_bank_id'=>$this->input->post('bank_id')
				);
			$this->Order_model->editOrder($id,$data);
			
			redirect('backend/order/detail/'.$id.'/success');
		}
	}
	
	function edit_detail()
	{
		if($this->input->post('jml') != ''):
			$id_prod = $this->uri->segment(6);
			$jml = mysql_escape_string($this->input->post('jml'));
			$prod = $this->Produk_model->getProductById($id_prod);
			$jml_stok = $prod->jml_stok;
			$itung = $jml_stok - $jml;
			if ($itung<0):
				$order_id = $this->uri->segment(5);
				$this->session->set_flashdata('message_type','<div id="kotak" class="error" >Jumlah melebihi persediaan</div>');
				redirect('backend/order/detail/'.$order_id);
			else:
				$order_id = $this->uri->segment(5);
				$order_det_id = $this->uri->segment(4);
				$prod_id = $this->uri->segment(6);
				$jmlNew = mysql_escape_string($this->input->post('jml'));
				
				$this->Order_model->editOrderDetail($order_id,$order_det_id,$jmlNew,$prod_id);
				$this->session->set_flashdata('message_type','<div id="kotak" class="success" >Data berhasil di update</div>');
				redirect('backend/order/detail/'.$order_id);
			endif;
		else:
			$order_id = $this->uri->segment(4);
			$this->session->set_flashdata('message_type','<div id="kotak" class="error" >Field jumlah tidak boleh kosong</div>');
			redirect('backend/order/detail/'.$order_id);
		endif;
	
	}


	function ajax_bulx_action(){
		if(isset($_REQUEST['checks']) && is_array($_REQUEST['checks'])){
			foreach($_REQUEST['checks'] as $pc){
				$pc = mysql_escape_string(intval($pc));
				if(isset($pc) && !empty($pc)){
					$this->order_model->deleteOrder( $pc );
				}
			}
		} else if(isset($_REQUEST['checks']) && !empty($_REQUEST['checks'])){
			$this->order_model->deleteOrder(mysql_escape_string(intval($_REQUEST['checks'])));
		}
		$msg = array('message0'=>"Order has been deleted") ;
		$this->session->set_flashdata($msg);
		redirect("backend/order/".$_REQUEST['status']);
	}
	
	function delete($id)
	{
		$id = $this->uri->segment(4);
		$uri = $this->uri->segment(5);
		$this->Order_model->deleteOrder($id);
		$this->Order_model->deleteOrderDetail($id);
		$count = count($this->Order_model->getOrders('all',FALSE,FALSE,FALSE));
		getPage('order','delete',$uri,$count);
	}




	function order_closed(){
		$this->data['status']='order_closed';

		$this->data['pagination']['base_url']=site_url().'backend/order/order_closed';
		$this->data['pagination']['total_rows']=count($this->order_model->getAllOrder($this->data['status']));
		$this->data['pagination']['per_page']=10;
		$this->pagination->initialize($this->data['pagination']);

		$this->data['order']=&$this->order_model->getOrders($this->data['status'],$this->data['pagination']['per_page'],mysql_escape_string($this->uri->segment(4)));

		$this->load->view('backend/order/orderlist',$this->data);
	}

	function order_trash(){
		$this->data['status']='order_trash';

		$this->data['pagination']['base_url']=site_url().'backend/order/order_trash';
		$this->data['pagination']['total_rows']=count($this->order_model->getAllOrder($this->data['status']));
		$this->data['pagination']['per_page']=10;
		$this->pagination->initialize($this->data['pagination']);

		$this->data['order']=&$this->order_model->getOrders($this->data['status'],$this->data['pagination']['per_page'],mysql_escape_string($this->uri->segment(4)));

		$this->load->view('backend/order/orderlist',$this->data);
	}
}